package org.umss.fixturebuilder.resources;

import org.umss.fixturebuilder.model.json.JsonPhaseList;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.umss.fixturebuilder.builder.model.Phase;
import org.umss.fixturebuilder.controller.PhaseController;
import org.umss.fixturebuilder.model.json.JsonNewPhase;

/**
 *
 * @author Daniela Meneses
 */
@Path("phase")
public class PhaseResource {

    private static final String ERROR_MESSAGE = "The phase can't be created.";

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
    public Response create(JsonNewPhase newPhaseParams) {

        Phase generatedPhase = PhaseController.create(newPhaseParams);

        return generatedPhase != null ? Response.status(Response.Status.CREATED).type(MediaType.APPLICATION_JSON).entity(generatedPhase).build()
                : Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.TEXT_PLAIN).entity(ERROR_MESSAGE).build();
    }

    @GET
    @Path("/all/{championshipId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAll(@PathParam("championshipId") String championshipId) {
        JsonPhaseList phases = PhaseController.getAll(championshipId);
        return phases != null ? Response.status(Response.Status.OK).entity(phases).build()
                : Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ERROR_MESSAGE).build();
    }
}
